/*
 * 作者: 侯晓宇
 * 时间: 2017-01-11 
 * 说明：
 * Copyright (c) 2016-2017 中研沃兮
 */
define(function(require) {
	require("jquery");
	var appInfo = require('app/AppInfo');
	var c = require('app/utils/CommonUtils');
	var hc = require('app/utils/HttpClientUtils');
	var tip = require('app/utils/TipUtils');
	var dbu = require('app/utils/DBUtils');
	var UserInfoUtils = require('app/utils/UserInfoUtils');
	var EventUtils = require('app/utils/EventUtils');
	var APIUtils = require('app/utils/APIUtils');
	
	var win = window;

	var action = {};
	var tabBar_api = api.require('NVTabBar');

	win.navbars = [];
	win.nav_now = 1; //默认显示的tab
	win.tab_now = 0;
	win.header_h = 0;
	win.footer_h = 0;

	action.setHeaderH = function(h){
		win.header_h = h;
	}
	action.setFooterH = function(h) {
		win.footer_h = h;
	}

	action.generateNavbar = function(name, uuid, num) {
		navbars.push({
			title: name,
			bg: '#FFFFFF',
			bgSelected: '#FFFFFF',
			//自定义数据
			uuid: uuid,
			num: num
		});
	};

	action.openNavbar = function() {
		var that = this;
		var h = win.header_h;
		var NVNavigationBar = api.require('NVNavigationBar');
		NVNavigationBar.open({
			rect: {
				x: h,
				y: 0,
				w: api.frameWidth - h,
				h: h
			},
			styles: {
				orientation: 'horizontal',
				bg: '#ffffff',
				bgAlpha: 1,
				font: {
					size: 14,
					sizeSelected: 18,
					color: 'rgb(60,60,60)',
					colorSelected: '#ff6a32',
					alpha: 1
				},
				itemSize: {
					w: 55,
					h: h
				}
			},
			items: navbars,
			selectedIndex: 1,
			fixedOn: api.frameName,
			id: 'navigationBar'
		}, function(ret, err) {
			if (ret.eventType == "click") {
				action.openCatalogByIndex(ret.index);
			} else {
				action.openCatalogByIndex(1);
			}
		});

	}
	action.openCatalogByUUID = function(cataloguuid, reload) {

		var name = "IndexCatalogFrm";
		var showBanner = false;
		var _reload = true;
		if(c.isNull(reload)){
			_reload = true
		}else{
			_reload = reload;
		}
		
		if(appInfo.uuid_catalog_follow == cataloguuid){
			if(!UserInfoUtils.isLogined()){
			//未登陆则打开LoginWin.js
				api.openWin({
			        name: 'LoginAndRegistWin',
			        url: './LoginAndRegistWin.html'
		        });
		        return;
			}
		}
		
		if (navbars[1].uuid == cataloguuid) {
			showBanner = true;
		}

		api.openFrame({
			name: name,
			url: './' + name + '.html',
			bounces: true,
			rect: {
				x: 0,
				y: win.header_h + 1,
				w: APIUtils.winW(),
				h: APIUtils.winH() - win.header_h - win.footer_h - 1
			},
			pageParam: {
				catalogUuid: cataloguuid,
				showBanner: showBanner
			},
			reload: _reload
		});

	}
	action.openCatalogByIndex = function(index, reload) {
		var uuid = navbars[index].uuid;
		action.openCatalogByUUID(uuid, reload);
	}
	action.getCatalogs = function() {
		var that = this;
		var promise = new Promise(function(resolve, reject) {

			hc.post(appInfo.serverbase + "/wjt/catalogs_app", {
				appid: appInfo.appid
			}, function(resp) {
				if (resp.status != "200") {
					//					reject(resp.result);
					return;
				}
				//成功
				resolve(resp.result);

			});
			//--end post
		});
		return promise;
	}

	action.openTab = function() {
		
		tabBar_api.open({
			styles: {
				bg: '#fff',
				h: 50,
				dividingLine: {
					width: 0.5,
					color: 'rgb(237,116,55)'
				},
				badge: {
					bgColor: 'rgb(237,116,55)',
					numColor: '#fff',
					size: 6.0
				}
			},
			items: [{
				w: APIUtils.winW() / 3.0,
				bg: {
					marginB: 0,
					image: 'rgba(0,0,0,0)'
				},
				iconRect: {
					w: 25.0,
					h: 25.0,
				},
				icon: {
					normal: 'widget://res/tabbar/home.png',
					highlight: 'widget://res/tabbar/home-active.png',
					selected: 'widget://res/tabbar/home-active.png'
				},
				title: {
					text: '主页',
					size: 12.0,
					normal: '#696969',
					selected: 'rgb(237,116,55)',
					marginB: 4.0
				}
			}, {
				w: APIUtils.winW() / 3.0,
				bg: {
					marginB: 0,
					image: 'rgba(0,0,0,0)'
				},
				iconRect: {
					w: 49.0,
					h: 49.0
				},
				icon: {
					normal: 'widget://res/tabbar/zhibo_button.png',
					highlight: 'widget://res/tabbar/zhibo_button.png',
					selected: 'widget://res/tabbar/zhibo_button.png'
				},
				title: {
					text: '',
					size: 0,
					normal: '#696969',
					selected: 'rgb(237,116,55)',
					marginB: 0
				}

			}, {
				w: APIUtils.winW() / 3.0,
				bg: {
					marginB: 0,
					image: 'rgba(0,0,0,0)'
				},
				iconRect: {
					w: 25.0,
					h: 25.0,
				},
				icon: {
					normal: 'widget://res/tabbar/user.png',
					highlight: 'widget://res/tabbar/user-active.png',
					selected: 'widget://res/tabbar/user-active.png'
				},
				title: {
					text: '我',
					size: 12.0,
					normal: '#696969',
					selected: 'rgb(237,116,55)',
					marginB: 4.0
				}
			}],
			selectedIndex: 0
		}, function(ret, err) {

			if (ret.eventType == "click") {
				win.tab_now = ret.index;

				if (ret.index == 0) {
					api.closeFrame({
					    name: 'IndexMeFrm'
					});
					action.openCatalogByIndex(win.nav_now, false);

				} else if (ret.index == 1) {
				
					if(!UserInfoUtils.isLogined()){
					//未登陆则打开LoginWin.js
						api.openWin({
					        name: 'LoginAndRegistWin',
					        url: './LoginAndRegistWin.html'
				        });
				        
				        
						tabBar_api.setSelect({
							index: 0
						});
				        return;
					}
					
					//直播开始
					var name = 'CreateRoomWin';
					api.openWin({
						name: name,
						url: './' + name + '.html',
						bounces: false,
						reload: true,
						animation:{
							type:"reveal",                //动画类型（详见动画类型常量）
						    subType:"from_bottom",       //动画子类型（详见动画子类型常量）
					    	duration:300                //动画过渡时间，默认300毫秒
						}
					});

				} else if (ret.index == 2) {

					
					if(!UserInfoUtils.isLogined()){
					//未登陆则打开LoginWin.js
						api.openWin({
					        name: 'LoginAndRegistWin',
					        url: './LoginAndRegistWin.html'
				        });
				        
						tabBar_api.setSelect({
							index: 0
						});
				        return;
					}
					//我
					api.openFrame({
						rect: {
							x: 0,
							y: 0,
							w: APIUtils.winW(),
							h: APIUtils.winH() - win.footer_h
						},
						name: 'IndexMeFrm',
						url: './IndexMeFrm.html',
						bounces: false,
						reload: false,
					});

				}



			}
			//			openTabBar_callback(ret);
		});

	}
	
	action.listenReset = function(){
		
		EventUtils.listen(api.winName,'reset',false,function(){
			api.closeToWin({
		        name: 'root'
	        });
			api.closeFrame({
			    name: 'IndexMeFrm'
			});
			tabBar_api.setSelect({
				index: 0
			});
			action.openCatalogByIndex(win.nav_now, false);
		});
	}

	return action;
});